# 购物车信息管理视图
from django.http import JsonResponse

from myadmin.models import Product


def add(request):
    """ 添加购物车操作 """
    # 从session中获取购物车信息
    cartlist = request.session.get('cartlist', {})
    # 获取要购买的菜品信息
    pid = request.GET.get("pid", None)
    if pid is not None:
        product = Product.objects.get(id=pid).toDict()
        product['num'] = 1  # 初始化当前菜品的购买量
        # 判断当前购物车中是否存在要放进购物车的菜品
        if pid in cartlist:
            cartlist[pid]['num'] += product['num']  # 增加购买量
        else:
            cartlist[pid] = product  # 放进购物车
        request.session['cartlist'] = cartlist
    return JsonResponse({'cartlist': cartlist})


def clear(request):
    """ 清空购物车操作 """
    request.session['cartlist'] = {}
    return JsonResponse({'cartlist': {}})


def change(request):
    """ 更改购物车信息操作 """
    cartlist = request.session.get('cartlist', {})
    pid = request.GET.get("pid", 0)  # 获取要修改的菜品id
    m = int(request.GET.get('num', 1))  # 要修改的数量
    if m < 1:
        m = 1
    cartlist[pid]['num'] = m  # 修改购物中的数量
    request.session['cartlist'] = cartlist
    return JsonResponse({'cartlist': cartlist})
